package com.demo.blog;

import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.ehcache.CacheInterceptor;
import com.jfinal.plugin.ehcache.CacheName;
import com.jfinal.render.JsonRender;

/**
 * BlogController 所有 sql 与业务逻辑写在 Model 或 Service 中，不要写在 Controller
 * 中，养成好习惯，有利于大型项目的开发与维护
 */
@Before(BlogInterceptor.class)
public class BlogController extends Controller {
	
	public void index() {
		setAttr("blogPage", Blog.me.paginate(getParaToInt(0, 1), 10));
		render("blog.html");
	}

	@RequiresPermissions("blog:test")
	public void getJson() {
		Map<String, String[]> map = getParaMap();
		System.out.println(map);
		renderNull();
		render(new JsonRender("{\"name\":\"测试\"}").forIE());
	}
	public void json() {
 
		Object id = 2;
		Blog b = Blog.me.findById(id);
		// renderJson(b);
		render(new JsonRender(b).forIE());
	}
	@RequiresPermissions("blog:test")
	public void add() {
	}

	@Before(BlogValidator.class)
	public void save() {
		// Oracle
		// new User().set("id", "my_seq.nextval")
		getModel(Blog.class).save();
		redirect("/blog");
	}

	public void edit() {
		setAttr("blog", Blog.me.findById(getParaToInt()));
	}

	@Before(BlogValidator.class)
	public void update() {
		getModel(Blog.class).update();
		redirect("/blog");
	}

	public void delete() {
		Blog.me.deleteById(getParaToInt());
		redirect("/blog");
	}
}
